** matching gas stations and weather stations

clear
import delimited "$P_Data/Germany Weather/Station_data.csv", encoding(UTF-8) 

drop sdo_name 
drop metadata_link

cross using  "$P_Data/GS/gas_station.dta"

keep sdo_id geogr_laenge geogr_breite hoehe_ueber_nn StID Lat Lng 

rename geogr_breite weather_lat
rename geogr_laenge weather_lng

drop if Lat==.

geodist weather_lat weather_lng Lat Lng, generate(distance)

sort StID distance
by StID: keep if _n==1


rename distance weather_distance
keep StID sdo_id hoehe_ueber_nn weather_distance

save "$P_Data_Processed/weather_concordance.dta", replace

**** creating daily weather data

** mean temperature
import delimited "$P_Data/Germany Weather/data_TMK_MN004_mean_temp.csv", clear 

keep sdo_id zeitstempel wert
rename zeitstempel daily_date
rename wert temperature

save "$P_Data_Processed/weather_data.dta", replace

** precipitation
import delimited "$P_Data/Germany Weather/data_RS_MN006_precipitation.csv", clear 
keep sdo_id zeitstempel wert
rename zeitstempel daily_date
rename wert precipitation

merge 1:1 sdo_id daily_date using "$P_Data_Processed/weather_data.dta"
drop _merge

save "$P_Data_Processed/weather_data.dta", replace


tostring daily_date, replace force
gen date=date(daily_date, "YMD")
drop daily_date

save "$P_Data_Processed/weather_data.dta", replace



